<template>
    <div id="seller" ref="sellerScroll">
        <div class="seller-content">
            <div class="overview">
                <h1 class="title">{{seller.name}}</h1>
                <div class="desc border-1px">
                    <ry-star :size="36" :score="seller.score"></ry-star>
                    <span class="text">排名({{seller.ratingCount}})</span>
                    <span class="text">月售{{seller.sellCount}}单</span>
                </div>
                <div class="favorite">
                    <span class="icon-favorite" :class="{'active': favorite}" @click="toggleCollection"></span>
                    <span class="text">{{favoriteText}}</span>
                </div>
                <div class="remark">
                    <ul class="remark-list">
                        <li class="block">
                            <h2 class="title">起送价</h2>
                            <div class="content">
                                <span class="stress">{{seller.minPrice}}</span>元
                            </div>
                        </li>
                        <li class="block">
                            <h2 class="title">商家配送费</h2>
                            <div class="content">
                                <span class="stress">{{seller.deliveryPrice}}</span>元
                            </div>
                        </li>
                        <li class="block">
                            <h2 class="title">平均配送时间</h2>
                            <div class="content">
                                <span class="stress">{{seller.deliveryTime}}</span>分钟
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
            <ry-split></ry-split>
            <div class="bulletin">
                <h1 class="title">公告与活动</h1>
                <div class="content-wrapper border-1px">
                    <p class="content">{{seller.bulletin}}</p>
                </div>
                <ul class="supports">
                    <li v-for="item in seller.supports" class="support-item border-1px">
                        <ry-class-map :type="item.type" :imageType="4"></ry-class-map>
                        <span class="text">{{item.description}}</span>
                    </li>
                </ul>
            </div>
            <ry-split v-if="seller.pics"></ry-split>
            <div v-if="seller.pics" class="pics">
                <h1 class="title">商家实景</h1>
                <div class="pic-wrapper" ref="picScroll">
                    <ul class="pic-list" ref="picList">
                        <li v-for="item in seller.pics" class="pic-item">
                            <img :src="item" />
                        </li>
                    </ul>
                </div>
            </div>
            <ry-split v-if="seller.infos"></ry-split>
            <div v-if="seller.infos" class="info">
                <h1 class="title">商家信息</h1>
                <ul class="info-list">
                    <li v-for="item in seller.infos" class="info-item border-1px">{{item}}</li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
    import BScroll from 'better-scroll'
    import RyStar from './../star/Star'
    import RySplit from './../split/Split'
    import RyClassMap from './../classMap/ClassMap'

    const PICWIDTH = 120;
    const PICMARGIN = 6;

    function defaultFavorite() {
        if(localStorage.hasOwnProperty('undefined')) {
            return JSON.parse(localStorage.getItem('undefined')).favorite;
        }else {
            localStorage.setItem('undefined', JSON.stringify({'favorite': false}));
            return false;
        }
    }

    export default {
        name: 'Seller',
        props: ['seller'],
        components: {
            RyStar,
            RySplit,
            RyClassMap
        },
        data() {
            return {
                favorite: defaultFavorite()
            }
        },
        methods: {
            _initScroll() {
                this.$nextTick(() => {
                    this.scroll ? this.scroll.refresh() : this.scroll = new BScroll(this.$refs.sellerScroll, {click: true});
                })
            },
            _picListHeight() {
                this.$nextTick(() => {
                    if(this.seller.pics || this.seller.pics.length) {
                        let width = (PICWIDTH + PICMARGIN) * this.seller.pics.length - 6; 
                        this.$refs.picList.style.width = `${width}px`;
                        this.picScroll ? this.picScroll.refresh() : this.picScroll = new BScroll(this.$refs.picScroll, {scrollX: true});
                    }
                })
            },
            toggleCollection() {
                localStorage.setItem('undefined', JSON.stringify({'favorite': !this.favorite}));
                this.favorite = !this.favorite;
            }
        },
        computed: {
            favoriteText() {
                return this.favorite ? '已收藏' : '收藏';
            }
        },
        created() {
            this._initScroll();
            this._picListHeight();
        }
    }
</script>

<style scoped>
    #seller {
        position: absolute;
        top: 174px;
        bottom: 0;
        left: 0;
        width: 100%;
        overflow: hidden;
    }

    .seller-content {
        overflow: hidden;
    }

    .overview {
        padding: 18px;
        position: relative;
    }

    .overview .title {
        color: #333;
        font-size: 14px;
        line-height: 14px;
        margin-bottom: 8px;
    }

    .desc {
        display: flex;
        padding-bottom: 18px;
        align-items: center;
        position: relative;
    }

    .desc::after {
        content: '';
        width: 100%;
        display: block;
        position: absolute;
        left: 0;
        bottom: 0;
        border-bottom: 1px solid rgba(7, 17, 27, .1);
    }

    #star {
        margin-right: 8px;
    }

    .desc .text {
        color: #666;
        font-size: 10px;
        line-height: 18px;
        margin-right: 12px;
    }

    .favorite {
        position: absolute;
        right: 11px;
        text-align: center;
        top: 18px;
        width: 50px;
    }
    
    .icon-favorite {
        color: #ccc;
        display: block;
        font-size: 24px;
        line-height: 24px;
        margin-bottom: 4px;
    }

    .icon-favorite.active {
        color: #f01414;
    }

    .favorite .text {
        color: #666;
        font-size: 10px;
        line-height: 10px;
    }

    .remark-list {
        display: flex;
        padding-top: 18px;
    }

    .block {
        flex: 1;
        text-align: center;
    }

    .block:nth-child(2) {
        position: relative;
    }

    .block:nth-child(2)::before {
        content: '';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        border-left: 1px solid rgba(7, 17, 27, .1);
    }

    .block:nth-child(2)::after {
        content: '';
        display: block;
        position: absolute;
        right: 0;
        top: 0;
        height: 100%;
        border-right: 1px solid rgba(7, 17, 27, .1);
    }

    .block .title {
        color: #999;
        font-size: 10px;
        line-height: 10px;
        margin-bottom: 4px;
    }

    .content {
        color: #333;
        font-size: 10px;
        line-height: 24px;
    }

    .stress {
        font-size: 24px;
    }

    .bulletin {
        padding: 18px 18px 0;
    }

    .bulletin .title {
        color: #333;
        font-size: 14px;
        line-height: 14px;
        margin-bottom: 8px;
    }
    
    .content-wrapper {
        padding: 0 12px 16px;
        position: relative;
    }

    .content-wrapper::after, 
    .support-item::after, 
    .info-item::after {
        content: '';
        display: block;
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        border-bottom: 1px solid rgba(7, 17, 27, .1);
    }

    .content-wrapper .content {
        color: #f01414;
        font-size: 12px;
        line-height: 24px;
    }

    .support-item {
        display: flex;
        padding: 16px 12px;
        align-items: center;
        position: relative;
    }

    .icon {
        width: 16px !important;
        height: 16px !important;
        margin-right: 6px !important;
    }

    .support-item:last-child::after, 
    .info-item:last-child::after {
        display: none;
    }

    .support-item .text {
        color: #333;
        font-size: 12px;
        line-height: 16px;
    }

    .pics {
        padding: 18px;
    }

    .pics .title {
        color: #333;
        font-size: 14px;
        line-height: 14px;
        margin-bottom: 12px;
    }

    .pic-wrapper {
        overflow: hidden;
    }

    .pic-list {
        font-size: 0;
        white-space: nowrap;
    }

    .pic-item {
        display: inline-block;
        height: 90px;
        width: 120px;
    }

    .pic-item + .pic-item {
        margin-left: 6px;
    }

    .pic-item img {
        height: 100%;
        width: 100%;
    }

    .info {
        color: #333;
        padding: 18px 18px 0;
    }

    .info .title {
        font-size: 14px;
        line-height: 14px;
        padding-bottom: 12px;
    }

    .info-item {
        font-size: 12px;
        line-height: 16px;
        padding: 16px 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        position: relative;
    }

    @media (min-device-pixel-ratio: 1.5), (-webkit-min-device-pixel-ratio: 1.5) {
        .block:nth-child(2)::before, 
        .block:nth-child(2)::after {
            transform: scaleX(.7);
        }
    }

    @media (min-device-pixel-ratio: 2), (-webkit-min-device-pixel-ratio: 2) {
        .block:nth-child(2)::before, 
        .block:nth-child(2)::after {
            transform: scaleX(.5);
        }
    }
</style>